package com.lc.es.service;

import com.lc.es.model.Position;
import com.lc.es.vo.PageResult;
import com.lc.es.vo.PositionRequest;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liuc
 * @date 2021/8/30
 * @description
 **/
@Service
public class PositionService {

    @Autowired
    private RestHighLevelClient client;

    public PageResult<Map<String,Object>> page(PositionRequest request) throws IOException {
        SearchRequest req = new SearchRequest("position");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        int pageNo = request.getPageNo();
        int size = request.getPageSize();
        sourceBuilder.from((pageNo-1)*size);
        sourceBuilder.size(size);
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("positionName",request.getContent());
        sourceBuilder.query(queryBuilder);
        req.source(sourceBuilder);
        SearchResponse response = client.search(req, RequestOptions.DEFAULT);
        TotalHits totalHits = response.getHits().getTotalHits();
        SearchHit[] hits = response.getHits().getHits();
        PageResult<Map<String, Object>> result = new PageResult<>();
        List<Map<String,Object>> list = new ArrayList<>();
        if(hits != null){
            for (SearchHit hit : hits) {
                list.add(hit.getSourceAsMap());
            }
        }
        long total = totalHits.value;
        if(total < 5){
            //不足5条,查询
            list.addAll(fill((int)(5-total)));
        }
        result.setData(list);
        result.setPageNo(pageNo);
        request.setPageSize(size);
        return result;
    }

    public List<Map<String, Object>> fill(int size) throws IOException {
        SearchRequest req = new SearchRequest("position");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.from(0);
        sourceBuilder.size(size);
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("positionAdvantage","美女多,员工福利好");
        sourceBuilder.query(queryBuilder);
        req.source(sourceBuilder);
        SearchResponse response = client.search(req, RequestOptions.DEFAULT);
        TotalHits totalHits = response.getHits().getTotalHits();
        SearchHit[] hits = response.getHits().getHits();
        List<Map<String,Object>> list = new ArrayList<>();
        if(hits != null){
            for (SearchHit hit : hits) {
                list.add(hit.getSourceAsMap());
            }
        }
        return list;
    }
}
